package com.labichaoka.chaoka.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.labichaoka.chaoka.R;

import java.util.ArrayList;
import java.util.List;

public class AutoEmailTextView extends android.support.v7.widget.AppCompatAutoCompleteTextView {

  private static final String[] emailSuffix = {"@qq.com", "@163.com", "@126.com", "@sina.com",
    "@yahoo.cn", "@sohu.com", "@163.net"};

  public AutoEmailTextView(Context context) {
    super(context);
    init(context);
  }

  public AutoEmailTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
  }

  private void init(Context context) {
    final MyAdatper adapter = new MyAdatper(context);
    setAdapter(adapter);
    addTextChangedListener(new TextWatcher() {
      @Override
      public void afterTextChanged(Editable s) {
        String input = s.toString();

        Log.d("aaa",input);

        adapter.mList.clear();
        if (input.length() > 0) {
          for (int i = 0; i < emailSuffix.length; ++i) {
            if (!input.contains("@")){
              adapter.mList.add(input + emailSuffix[i]);
            }

          }
        }
        adapter.notifyDataSetChanged();

      }

      @Override
      public void beforeTextChanged(CharSequence s, int start, int count,
                                    int after) {

      }

      @Override
      public void onTextChanged(CharSequence s, int start, int before,
                                int count) {
      }
    });

    setThreshold(1);
  }
  class MyAdatper extends BaseAdapter implements Filterable {
    List<String> mList;
    private Context mContext;
    private MyFilter mFilter;

    public MyAdatper(Context context) {
      mContext = context;
      mList = new ArrayList<>();
    }

    @Override
    public int getCount() {
      return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
      return mList == null ? null : mList.get(position);
    }

    @Override
    public long getItemId(int position) {
      return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

      View view;
      ViewHolder holder;
      if(convertView==null){
        view = View.inflate(mContext, R.layout.item_email, null);
        holder = new ViewHolder();
        holder.title =view.findViewById(R.id.title);
        view.setTag(holder);
      }else{
        view = convertView;
        holder = (ViewHolder) view.getTag();
      }
      String pc=mList.get(position);
      holder.title.setText(pc);
      return view;
    }

    class ViewHolder{
       TextView title;
    }

    @Override
    public Filter getFilter() {
      if (mFilter == null) {
        mFilter = new MyFilter();
      }
      return mFilter;
    }

    private class MyFilter extends Filter {

      @Override
      protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if (mList == null) {
          mList = new ArrayList<>();
        }
        results.values = mList;
        results.count = mList.size();
        return results;
      }

      @Override
      protected void publishResults(CharSequence constraint, FilterResults results) {
        if (results.count > 0) {
          notifyDataSetChanged();
        } else {
          notifyDataSetInvalidated();
        }
      }
    }
  }

}
